<?php


namespace app\plugin\controller\merchant;


use app\common\controller\AppCommon;
use app\plugin\controller\merchant\com\MerchantManager;
use app\service\MerchantMsg;
use app\service\Page;

class Msg extends MerchantManager
{
    public function index()
    {
        if (IS_AJAX) {
            if (!empty($this->param['id'])) {
                $data = AppCommon::data_get('plugin_merchant_msg', ['id' => intval($this->param['id'])]);
                if (empty($data)) {
                    data_return('消息不存在', -1);
                }
                data_return('success', 0, $data);
            }
        }
        $where = [
            'uid' => $this->m_uid
        ];
        $orderBy = 'id desc';
        $page = !empty($this->param['page']) ? intval($this->param['page']) : 1;
        $pageSize = 10;

        $is_read = !empty($this->param['is_read']) ? intval($this->param['is_read']) : '';
        $keyword = !empty($this->param['keyword']) ? trim($this->param['keyword']) : '';

        if (!empty($keyword)) {
            $where['title'] = ['like', '%' . $this->param['keyword'] . '%'];
        }
        if ($is_read == 1) {
            $where['read_time'] = ['>', 0];
        } elseif ($is_read == -1) {
            $where['read_time'] = 0;
        }

        $total = AppCommon::data_count('plugin_merchant_msg', $where);
        $data = AppCommon::data_list('plugin_merchant_msg', $where, $page . ',' . $pageSize, '*', $orderBy);
        if (!empty($data)) {
            foreach ($data as &$v) {
                $v['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
                if ($v['type'] == '1') {
                    $v['msg_type_desc'] = '普通消息';
                } elseif ($v['msg_type'] == '2') {
                    $v['msg_type_desc'] = '系统消息';
                } else {
                    $v['msg_type_desc'] = '未分类';
                }
                $v['content'] = mb_substr($v['content'], 0, 15, 'utf8') . '...';
            }
            unset($v);
        }

        $this->assign('page', Page::set($data, $pageSize, $page, $total, $this->param, url()));
        $this->assign('data', $data);
        return $this->fetch();
    }

    //未读消息统计
    public function msg_count()
    {
        $total = AppCommon::data_count('plugin_merchant_msg', ['uid' => $this->m_uid, 'read_time' => 0]);
        data_return('ok', 0, [
            'total' => $total
        ]);
    }

    public function msg_action()
    {
        $ac = !empty($this->param['ac']) ? $this->param['ac'] : '';
        if (!$ac) {
            data_return('参数有误', -1);
        }
        $id = !empty($this->param['id']) ? intval($this->param['id']) : 0;
        if (!empty($id)) {
            $data = AppCommon::data_get('plugin_merchant_msg', ['id' => $id, 'uid' => $this->m_uid]);
            if (empty($data)) {
                data_return('参数有误', -1);
            }
            if ($ac == 'readstate') {
                AppCommon::data_update('plugin_merchant_msg', ['uid' => $this->m_uid, 'id' => $id], ['read_time' => empty($data['read_time']) ? time() : 0]);
            }
        } elseif ($ac == 'del_all') {
            if (empty($this->param['values'])) {
                data_return('请先选记录', -1);
            }
            $ids = array_unique($this->param['values']);
            array_walk($ids, function (&$id) {
                $id = intval($id);
            });
            AppCommon::data_del('plugin_merchant_msg', ['uid' => $this->m_uid, 'id' => ['in', $ids]]);
        } elseif ($ac == 'read_all') {
            if (empty($this->param['values'])) {
                data_return('请先选记录', -1);
            }
            $ids = array_unique($this->param['values']);
            array_walk($ids, function (&$id) {
                $id = intval($id);
            });

            AppCommon::data_update('plugin_merchant_msg', ['uid' => $this->m_uid, 'id' => ['in', $ids]], ['read_time' => time()]);
        }

        data_return('操作成功');
    }

}